﻿#include "Job.h"
#include <vector>

vector<Job> Subtime_sort(vector<Job> job_set)
{
	int i, j;
	for(i=0;i<job_set.size();i++)
		for (j = i + 1; j < job_set.size(); j++)
		{
			if (job_set[i].get_sub_time() > job_set[j].get_sub_time())
			{
				Job temp(job_set[i]);
				job_set[i] = job_set[j];
				job_set[j] = temp;
			}
		}
	return job_set;
}

void FCFS(vector<Job> job_set)
{
	int i, j;
	Subtime_sort(job_set);

	Time start_time = job_set[0].get_sub_time();

	cout << "\n\n* * * * * * * 先来先服务算法FCFS * * * * * * *" << endl << endl;

	for (i = 0; i < job_set.size(); i++)
	{
		if (start_time < job_set[i].get_sub_time())
			start_time = job_set[i].get_sub_time();

		// 赋予等待时间
		job_set[i].set_wait_time(Time2int(start_time - job_set[i].get_sub_time()));

		// 赋予开始时间
		job_set[i].set_start_time(start_time);

		// 赋予结束时间
		start_time += int2Time(job_set[i].get_runtime());
		job_set[i].set_finish_time(start_time);

		job_set[i].set_whole_time(job_set[i].get_wait_time() + job_set[i].get_runtime());

		job_set[i].print_info();
	}
}

vector<Job> Runtime_sort(vector<Job> job_set)
{
	int i, j;
	for (i = 0; i < job_set.size(); i++)
		for (j = i + 1; j < job_set.size(); j++)
		{
			if (job_set[i].get_runtime() > job_set[j].get_runtime())
			{
				Job temp(job_set[i]);
				job_set[i] = job_set[j];
				job_set[j] = temp;
			}
		}
	return job_set;
}

void SJF(vector<Job> job_set)
{
	// 首先按照FCFS排序，如果有多个进程排队，则后续的按照最短优先
	int i, j;
	vector<Job> copy_set = Subtime_sort(job_set);

	Time start_time = copy_set[0].get_sub_time();

	cout << "\n\n* * * * * * * 最短作业优先SJF * * * * * * *" << endl << endl;

	i = 0;
	while (i++ < job_set.size())
	{
		if (start_time <= copy_set[0].get_sub_time())
			start_time = copy_set[0].get_sub_time();
		else
		{
			copy_set = Runtime_sort(copy_set);
		}

		for (j = 0; j < copy_set.size(); j++)
		{
			if (copy_set[j].get_sub_time() <= start_time)
				break;
		}
		// 赋予等待时间
		copy_set[j].set_wait_time(Time2int(start_time - copy_set[j].get_sub_time()));

		// 赋予开始时间
		copy_set[j].set_start_time(start_time);

		// 赋予结束时间
		start_time += int2Time(copy_set[j].get_runtime());
		copy_set[j].set_finish_time(start_time);

		copy_set[j].set_whole_time(copy_set[j].get_wait_time() + copy_set[j].get_runtime());

		copy_set[j].print_info();

		copy_set.erase(copy_set.begin() + j);

		for (int j = 0; j < copy_set.size(); j++)
		{
			copy_set[j].set_wait_time(Time2int(start_time - copy_set[j].get_sub_time()));
		}

		copy_set = Subtime_sort(copy_set);
	}
}

vector<Job> Ratio_sort(vector<Job> job_set)
{
	// RP = （等待时间+要求服务时间）/要求服务时间
	int i, j;
	for (i = 0; i < job_set.size(); i++)
		for (j = i + 1; j < job_set.size(); j++)
		{
			float ratio_i, ratio_j;
			ratio_i = 1.0 + float(job_set[i].get_wait_time()) / float(job_set[i].get_runtime());
			ratio_j = 1.0 + float(job_set[j].get_wait_time()) / float(job_set[j].get_runtime());

			if (ratio_i > ratio_j)
			{
				Job temp(job_set[i]);
				job_set[i] = job_set[j];
				job_set[j] = temp;
			}
		}
	return job_set;
}

void HRRN(vector<Job> job_set)
{
	// 首先按照FCFS排序，之后再动态更新
	int i, j;
	
	vector<Job> copy_set = Subtime_sort(job_set);
	Time start_time = copy_set[0].get_sub_time();

	cout << "\n\n* * * * * * * 最高响应比优先算法HRRN * * * * * * *" << endl << endl;
	i = 0;
	// RP = （等待时间+要求服务时间）/要求服务时间
	while(i++<job_set.size())
	{
		if (start_time <= copy_set[0].get_sub_time())
			start_time = copy_set[0].get_sub_time();
		else
		{
			copy_set = Ratio_sort(copy_set);
		}

		for (j = 0; j < copy_set.size(); j++)
		{
			if (copy_set[j].get_sub_time() <= start_time)
				break;
		}
		// 赋予等待时间
		copy_set[j].set_wait_time(Time2int(start_time - copy_set[j].get_sub_time()));

		// 赋予开始时间
		copy_set[j].set_start_time(start_time);

		// 赋予结束时间
		start_time += int2Time(copy_set[j].get_runtime());
		copy_set[j].set_finish_time(start_time);

		copy_set[j].set_whole_time(copy_set[j].get_wait_time() + copy_set[j].get_runtime());

		copy_set[j].print_info();

		copy_set.erase(copy_set.begin() + j);

		for (int j = 0; j < copy_set.size(); j++)
		{
			copy_set[j].set_wait_time(Time2int(start_time - copy_set[j].get_sub_time()));
		}

		copy_set = Subtime_sort(copy_set);
	}
}

int main()
{
	int job_num;
	cout << "请输入作业数量：";
	cin >> job_num;
	cout << endl;

	vector<Job> job_set;
	int i = 0;
	while (i < job_num)
	{
		Job new_job;
		new_job.init_from_input();
		job_set.push_back(new_job);
		i++;
	}
	
	while (true)
	{
		cout << "请选择调度算法：" << endl;
		cout << "1、FCFS 2、SJF 3、HRRN" << endl;
		int choose;
		cin >> choose;
		switch (choose)
		{
			case 1:system("cls"); FCFS(job_set); break;
			case 2:system("cls"); SJF(job_set); break;
			case 3:system("cls"); HRRN(job_set); break;
			default:exit(0);
		}
	}
	return 0;
}

/*----input----*/
/*
5
1      JA       02:40     20     
2      JB       02:50     30     
3      JC       02:55     10     
4      JD       03:00     24     
5      JE       03:05     6
*/